package cn.just.student_mall.controller;

import cn.just.student_mall.entity.Address;
import cn.just.student_mall.service.AddressService;
import cn.just.student_mall.service.ex.AddressCountMaxException;
import cn.just.student_mall.service.ex.InsertException;
import cn.just.student_mall.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @Author wujiaming
 * @Description TODO
 * @Date 2021-03-25 15:47
 * @Version 1.0
 */

@RequestMapping("addresses")
@RestController
public class AddressController {

    @Autowired
    AddressService addressService;

    public static final Integer OK = 2000;


    /**
     * /addresses/addNew
     * @param address
     * @param session
     * @return
     */
    @RequestMapping("addnew")
    public JsonResult<Void> addNew(Address address, HttpSession session){

        try{
            Integer uid = (Integer)session.getAttribute("uid");
            String username = (String) session.getAttribute("username");
            addressService.addNew(uid,username,address);
            return new JsonResult<>(OK);
        }catch(InsertException e){
            return new JsonResult<>(-1,"请联系管理员");
        }catch(AddressCountMaxException e){
            return new JsonResult<>("新增地址失败，收货地址数量已达到上限");
        }


    }
}
